import * as api from '@/services/ReturnMoneyPlanMgr';
import _ from 'lodash';
import moment from 'moment';
import { router } from 'umi';
import { getExpandedRowKeys, buildTree } from '../utils/publicMethod';

const Model = {
  namespace: 'ReturnMoneyPlan',
  state: {
    currentYear: null,
    overAllYears: [],
    currentMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'],
    all: [], // 原始数据
    treeData: [], // 表格数据
    expandedRowKeys: [], // 表格展开的id
    versionsList: [], // 版本列表
    fixedColumns: [],
    headerRenderer: null,
  },
  effects: {
    // 获取版本列表
    *getVersionsList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { versionsList: [], treeData: [] },
      });
      const { level1, level2, project, stage, category } = payload.match.params;
      const params = {
        stageId: stage, // 项目分期id
        versionType: category, // 版本类型
      };
      // 请求版本接口
      const response = yield call(api.returnPlanVersionDataListUsingGET, { params });
      if (response && response.code === 1 && !_.isEmpty(response.data)) {
        const url = `/OperatingPlan/ReturnMoneyPlan/${level1}/${level2}/${project}/${stage}/${category}/${response.data[0].versionId}`;
        router.replace(url);
        // 版本列表存储起来
        yield put({
          type: 'save',
          payload: { versionsList: response.data },
        });
      } else if (callback) callback();
    },
    // 获取年份列表
    *getAllYearsList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { overAllYears: [], currentYear: null },
      });
      // 请求年份列表数据
      const response = yield call(api.yearListUsingGET, payload);
      // 默认数据
      if (response && response.code === 1 && !_.isEmpty(response.data)) {
        let currentYear = null;
        // 筛选出当年
        const isYearValue = _.filter(response.data, x => x === `${moment().year()}`);
        // 如果没有当年就默认第一个
        if (!_.isEmpty(isYearValue)) {
          currentYear = _.first(isYearValue);
        } else {
          currentYear = _.first(response.data[0]);
        }
        yield put({
          type: 'save',
          payload: { overAllYears: response.data, currentYear },
        });
      } else if (callback) callback();
    },
    // 获取表格数据
    *getTableList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { all: [], treeData: [], expandedRowKeys: [] },
      });
      // 请求表格数据
      const response = yield call(api.annualDataUsingGET, payload);
      if (response && response.code === 1 && !_.isEmpty(response.data)) {
        const all = response.data;
        // 获取展开的列key
        const expandedRowKeys = getExpandedRowKeys(all);
        const treeData = buildTree(all);
        yield put({
          type: 'save',
          payload: { all, treeData, expandedRowKeys },
        });
      } else if (callback) callback();
    },
  },
  reducers: {
    // 存储切换年份
    changeCurrentYear(state, action) {
      return {
        ...state,
        currentYear: action.payload.currentYear,
      };
    },
    // 存储显示月份
    changeCurrentMonths(state, action) {
      let { currentMonths } = action.payload;
      if (currentMonths == null || currentMonths.length === 0) {
        currentMonths = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'];
      }
      return {
        ...state,
        currentMonths,
      };
    },
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
